package com.simblog.common.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

public class EmailUtils {
     
	private static final Logger logger = LoggerFactory.getLogger(EmailUtils.class);
	
    // 设置服务器
    private String smtpKey;
    private String smtpValue;
    // 服务器验证
    private String propsKey;
    private boolean propsValue;
    
    // 发送邮箱
    private String sendFrom;

    // 发件人用户名、密码
    private String username;
    private String password;
    
    // 会话
    private MimeMessage message;
    private Session session;
 
    /*
     * 构造方法
     */
    public EmailUtils() {
    	
    }
    
    /*
     * 初始化message
     */
    public void initMessage(){
        Properties props = System.getProperties();
        props.setProperty(smtpKey, smtpValue);
        props.put(propsKey, propsValue);
        session =  Session.getDefaultInstance(props, new Authenticator(){
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
              }});
        session.setDebug(true);
        message = new MimeMessage(session);
    }
 
    /**
     * 发送邮件
     * 
     * @param headName
     *            邮件头文件名
     * @param sendHtml
     *            邮件内容
     * @param receiveUser
     *            收件人地址
     */
    public void sendHtmlEmail(String title, String content,
            String receiveUser) {
        try {
            // 发件人
             InternetAddress from = new InternetAddress(sendFrom);
            message.setFrom(from);
            // 收件人
            InternetAddress to = new InternetAddress(receiveUser);
            message.setRecipient(Message.RecipientType.TO, to);
            // 邮件标题
            message.setSubject(title);
            // 邮件内容,也可以使纯文本"text/plain"
//            String content = sendHtml.toString();
            message.setContent(content, "text/html;charset=GBK");
            message.saveChanges();
            Transport transport = session.getTransport("smtp");
            // smtp验证，就是你用来发邮件的邮箱用户名密码
            transport.connect(smtpValue, username, password);
            // 发送
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            
            logger.info("发送邮件给{}成功.", receiveUser);
        } catch (SendFailedException  e) {
        	logger.info("收件邮箱地址{}有误.", receiveUser);
        } catch (Exception e) {
        	logger.info("邮件发送失败.\n"+e.getMessage());
        }
    }

	public void setSmtpKey(String smtpKey) {
		this.smtpKey = smtpKey;
	}

	public void setSmtpValue(String smtpValue) {
		this.smtpValue = smtpValue;
	}

	public void setPropsKey(String propsKey) {
		this.propsKey = propsKey;
	}

	public void setPropsValue(boolean propsValue) {
		this.propsValue = propsValue;
	}

	public void setSendFrom(String sendFrom) {
		this.sendFrom = sendFrom;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}
    
    
}